import { BasePanel } from "../base/BasePanel";
import { BaseComponent } from "../base/BaseComponent";
import { GameConfig } from "../config/GameConfig";
import { GameLayerManager } from "./GameLayerManager";
import { EventManager } from "./EventManager";

/**
  * 面板弹出管理类
  * by dily
  * (c) copyright 2014 - 2035
  * All Rights Reserved. 
  * 面板弹出的管理类
  */
export module PopUpManager {

	export var curPanel: BasePanel;
	// panel历史列表，不包含当前当前打开的panel
	var historyPanels: Array<BasePanel> = new Array<BasePanel>();

    /**
     * 添加面板方法
     * @param panel   面板
	 * @param isDestroyPre 是否销毁上个panel
     */
	export function openPanel(panel: BasePanel, isDestroyPre: boolean = false): void {
		let container: eui.UILayer = GameLayerManager.instance().panelLayer;

		if (curPanel) {
			if (isDestroyPre) {
				curPanel.dispose();
				curPanel = null;
			} else {
				curPanel.hide();
				historyPanels.push(curPanel);
			}
		}

		let fn = () => {
			panel.removeEventListener(eui.UIEvent.CREATION_COMPLETE, fn, this);
			panel.freshen();
		}
		curPanel = panel;
		panel.addEventListener(eui.UIEvent.CREATION_COMPLETE, fn, this);
		container.addChild(panel);
	}

	/**
	 * 关闭面板
	 * @param closeAll boolean 是否一同关闭panel的缓存历史
	 */
	export function closePanel(closeAll: boolean = false): void {
		let container: eui.UILayer = GameLayerManager.instance().panelLayer;

		if (container.numChildren > 0) {
			curPanel.removeChildren();
			container.removeChild(curPanel);
			EventManager.unRegistersAll(curPanel);
			egret.Tween.removeTweens(curPanel);
			curPanel = null;
			if (closeAll) {
				clearHistroyPanels();
			} else {
				if (historyPanels.length > 0) {
					let prePanel: BasePanel = historyPanels.pop();
					curPanel = prePanel;
					prePanel.freshen();
				}
			}
		} else {
			// container.visible = false;
		}
	}

	/**
	 * 清空历史面板
	 */
	function clearHistroyPanels(): void {
		let historys = historyPanels.splice(0);
		historys.forEach((panel) => {
			(<any>panel).dispose();
		});
		historys = null;
	}

	/**
	 * 打开Pop
	 */
	export function openPop(popup: BaseComponent): void {
		GameLayerManager.instance().popLayer.addChild(popup);
	}

	/** 
	 * 关闭Pop
	 */
	export function closePop(popup: BaseComponent): void {
		popup.dispose();
	}


	/** 弹窗特效 */
	function showEffect(panel, effectType): void {
		var popUpWidth = 0;
		var popUpHeight = 0;
		if (popUpWidth != 0) {
			panel.x = GameConfig.curWidth() / 2 - popUpWidth / 2;
			panel.y = GameConfig.curHeight() / 2 - popUpHeight / 2;
		} else {
			popUpWidth = panel.width;
			popUpHeight = panel.height;
		}

		//以下是弹窗动画
		var leftX: number = GameConfig.curWidth() / 2 - popUpWidth / 2;
		var upY: number = GameConfig.curHeight() / 2 - popUpHeight / 2;

		switch (effectType) {
			case 0:
				break;
			case 1:
				panel.alpha = 0;
				panel.scaleX = 0.5;
				panel.scaleY = 0.5;
				panel.x = panel.x + popUpWidth / 4;
				panel.y = panel.y + popUpHeight / 4;
				egret.Tween.get(panel).to({ alpha: 1, scaleX: 1, scaleY: 1, x: panel.x - popUpWidth / 4, y: panel.y - popUpHeight / 4 }, 300, egret.Ease.backOut);
				break;
			case 2:
				panel.alpha = 0;
				panel.scaleX = 0.5;
				panel.scaleY = 0.5;
				panel.x = panel.x + popUpWidth / 4;
				panel.y = panel.y + popUpHeight / 4;
				egret.Tween.get(panel).to({ alpha: 1, scaleX: 1, scaleY: 1, x: panel.x - popUpWidth / 4, y: panel.y - popUpHeight / 4 }, 600, egret.Ease.elasticOut);
				break;
			case 3:
				panel.x = - popUpWidth;
				egret.Tween.get(panel).to({ x: 0 }, 500, egret.Ease.cubicOut);
				break;
			case 4:
				panel.x = popUpWidth;
				egret.Tween.get(panel).to({ x: 0 }, 500, egret.Ease.cubicOut);
				break;
			case 5:
				panel.y = - popUpHeight;
				egret.Tween.get(panel).to({ y: 0 }, 500, egret.Ease.cubicOut);
				break;
			case 6:
				panel.y = popUpHeight;
				egret.Tween.get(panel).to({ y: 0 }, 500, egret.Ease.cubicOut);
				break;
			case 7:
				panel.alpha = 0;
				egret.Tween.get(panel).to({ alpha: 1 }, 1000);
				break;
			default:
				break;
		}
	}




}


